<template>
  <div class="role-management">
    <div class="page-header">
      <h1>角色管理</h1>
      <button @click="showAddRoleModal = true" class="btn-primary">
        <i class="icon-add"></i>
        添加角色
      </button>
    </div>

    <!-- 搜索和筛选区域 -->
    <div class="filter-section">
      <div class="search-box">
        <input 
          type="text" 
          placeholder="搜索角色名称、描述..." 
          v-model="searchQuery"
          @input="handleSearch"
        />
        <i class="icon-search"></i>
      </div>
      <div class="filter-options">
        <select v-model="statusFilter" @change="filterRoles">
          <option value="">全部状态</option>
          <option value="active">启用</option>
          <option value="inactive">禁用</option>
        </select>
      </div>
    </div>

    <!-- 角色列表表格 -->
    <div class="table-container">
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>角色名称</th>
            <th>角色代码</th>
            <th>描述</th>
            <th>状态</th>
            <th>创建时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="role in filteredRoles" :key="role.id">
            <td>{{ role.id }}</td>
            <td>{{ role.name }}</td>
            <td>{{ role.code }}</td>
            <td>{{ role.remark || role.description }}</td>
            <td>
              <span :class="['status-badge', role.status === 0 ? 'active' : 'inactive']">
                {{ getStatusText(role.status) }}
              </span>
            </td>
            <td>{{ formatDate(role.createdAt) }}</td>
            <td class="action-buttons">
              <button @click="viewRole(role)" class="btn-view" title="查看详情">
                <i class="icon-view"></i>
                查看
              </button>
              <button @click="editRole(role)" class="btn-edit" title="编辑">
                <i class="icon-edit"></i>
                编辑
              </button>
              <button @click="editPermissions(role)" class="btn-permissions" title="权限设置">
                <i class="icon-permissions"></i>
                权限
              </button>
              <button @click="bindMenus(role)" class="btn-menu-bind" title="角色绑定菜单">
                <i class="icon-menu-bind"></i>
                绑定菜单
              </button>
              <button 
                v-if="role.status === 0" 
                @click="disableRole(role)" 
                class="btn-disable" 
                title="禁用"
              >
                <i class="icon-disable"></i>
                禁用
              </button>
              <button 
                v-else 
                @click="enableRole(role)" 
                class="btn-enable" 
                title="启用"
              >
                <i class="icon-enable"></i>
                启用
              </button>
              <button @click="deleteRole(role)" class="btn-delete" title="删除">
                <i class="icon-delete"></i>
                删除
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 分页控件 -->
    <div class="pagination">
      <button 
        @click="prevPage" 
        :disabled="currentPage === 1"
        class="btn-page"
      >
        上一页
      </button>
      <span class="page-info">
        第 {{ currentPage }} 页，共 {{ totalPages }} 页
      </span>
      <button 
        @click="nextPage" 
        :disabled="currentPage === totalPages"
        class="btn-page"
      >
        下一页
      </button>
    </div>

    <!-- 添加/编辑角色模态框 -->
    <div v-if="showAddRoleModal || showEditRoleModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h2>{{ showEditRoleModal ? '编辑角色' : '添加角色' }}</h2>
          <button @click="closeModal" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="saveRole">
            <div class="form-group">
              <label for="name">角色名称</label>
              <input 
                type="text" 
                id="name" 
                v-model="roleForm.name" 
                required
              />
            </div>
            <div class="form-group">
              <label for="code">角色代码</label>
              <input 
                type="text" 
                id="code" 
                v-model="roleForm.code" 
                required
              />
            </div>
            <div class="form-group">
              <label for="remark">描述</label>
              <textarea 
                id="remark" 
                v-model="roleForm.remark" 
                rows="3"
              ></textarea>
            </div>
            <div class="form-group">
              <label for="status">状态</label>
              <select id="status" v-model="roleForm.status" required>
                <option value="0">启用</option>
                <option value="1">禁用</option>
              </select>
            </div>
            <div class="form-actions">
              <button type="button" @click="closeModal" class="btn-cancel">
                取消
              </button>
              <button type="submit" class="btn-primary">
                {{ showEditRoleModal ? '保存' : '添加' }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- 角色详情模态框 -->
    <div v-if="showRoleDetailModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h2>角色详情</h2>
          <button @click="showRoleDetailModal = false" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <div class="role-detail">
            <div class="detail-item">
              <span class="label">ID:</span>
              <span class="value">{{ selectedRole.id }}</span>
            </div>
            <div class="detail-item">
              <span class="label">角色名称:</span>
              <span class="value">{{ selectedRole.name }}</span>
            </div>
            <div class="detail-item">
              <span class="label">角色代码:</span>
              <span class="value">{{ selectedRole.code }}</span>
            </div>
            <div class="detail-item">
              <span class="label">描述:</span>
              <span class="value">{{ selectedRole.remark || selectedRole.description }}</span>
            </div>
            <div class="detail-item">
              <span class="label">状态:</span>
              <span :class="['status-badge', selectedRole.status === 0 ? 'active' : 'inactive']">
                {{ getStatusText(selectedRole.status) }}
              </span>
            </div>
            <div class="detail-item">
              <span class="label">创建时间:</span>
              <span class="value">{{ formatDate(selectedRole.createdAt) }}</span>
            </div>
            <div class="detail-item">
              <span class="label">更新时间:</span>
              <span class="value">{{ formatDate(selectedRole.updatedAt) }}</span>
            </div>
          </div>
          <div class="modal-actions">
            <button @click="showRoleDetailModal = false" class="btn-primary">
              关闭
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 权限设置模态框 -->
    <div v-if="showPermissionModal" class="modal-overlay">
      <div class="modal large-modal">
        <div class="modal-header">
          <h2>权限设置 - {{ selectedRole.name }}</h2>
          <button @click="showPermissionModal = false" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <div class="permission-section">
            <h3>菜单权限</h3>
            <div class="permission-tree">
              <div v-for="menu in menuPermissions" :key="menu.id" class="permission-item">
                <div class="permission-header">
                  <input 
                    type="checkbox" 
                    :id="`menu-${menu.id}`"
                    v-model="menu.checked"
                    @change="toggleMenuPermission(menu)"
                  />
                  <label :for="`menu-${menu.id}`">{{ menu.name }}</label>
                </div>
                <div v-if="menu.children && menu.children.length > 0" class="permission-children">
                  <div v-for="child in menu.children" :key="child.id" class="permission-item child">
                    <div class="permission-header">
                      <input 
                        type="checkbox" 
                        :id="`menu-${child.id}`"
                        v-model="child.checked"
                        @change="toggleMenuPermission(child)"
                      />
                      <label :for="`menu-${child.id}`">{{ child.name }}</label>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="permission-section">
            <h3>操作权限</h3>
            <div class="permission-grid">
              <div v-for="permission in operationPermissions" :key="permission.id" class="permission-checkbox">
                <input 
                  type="checkbox" 
                  :id="`op-${permission.id}`"
                  v-model="permission.checked"
                />
                <label :for="`op-${permission.id}`">{{ permission.name }}</label>
              </div>
            </div>
          </div>
          
          <div class="form-actions">
            <button @click="showPermissionModal = false" class="btn-cancel">
              取消
            </button>
            <button @click="savePermissions" class="btn-primary">
              保存权限
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 角色绑定菜单模态框 -->
    <div v-if="showMenuBindModal" class="modal-overlay">
      <div class="modal large-modal">
        <div class="modal-header">
          <h2>角色绑定菜单 - {{ selectedRole.name }}</h2>
          <button @click="showMenuBindModal = false" class="btn-close">&times;</button>
        </div>
        <div class="modal-body">
          <div class="menu-bind-section">
            <h3>菜单列表</h3>
            <div class="menu-tree">
              <div v-for="menu in menuTree" :key="menu.id" class="menu-item">
                <div class="menu-header">
                  <input 
                    type="checkbox" 
                    :id="`menu-${menu.id}`"
                    v-model="menu.checked"
                    @change="toggleMenuCheck(menu)"
                  />
                  <label :for="`menu-${menu.id}`">{{ menu.name }}</label>
                  <span :class="['type-badge', menu.type === 1 ? 'directory' : menu.type === 2 ? 'menu' : 'button']">
                    {{ getMenuTypeText(menu.type) }}
                  </span>
                </div>
                <div v-if="menu.children && menu.children.length > 0" class="menu-children">
                  <div v-for="child in menu.children" :key="child.id" class="menu-item child">
                    <div class="menu-header">
                      <input 
                        type="checkbox" 
                        :id="`menu-${child.id}`"
                        v-model="child.checked"
                        @change="toggleMenuCheck(child)"
                      />
                      <label :for="`menu-${child.id}`">{{ child.name }}</label>
                      <span :class="['type-badge', child.type === 1 ? 'directory' : child.type === 2 ? 'menu' : 'button']">
                        {{ getMenuTypeText(child.type) }}
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="form-actions">
            <button @click="showMenuBindModal = false" class="btn-cancel">
              取消
            </button>
            <button @click="saveMenuBindings" class="btn-primary">
              保存绑定
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { roleApi, menuApi } from '@/api/modules'

// 响应式数据
const roles = ref([])
const searchQuery = ref('')
const statusFilter = ref('')
const currentPage = ref(1)
const pageSize = ref(20)
const totalRoles = ref(0)

// 模态框状态
const showAddRoleModal = ref(false)
const showEditRoleModal = ref(false)
const showRoleDetailModal = ref(false)
const showPermissionModal = ref(false)
const showMenuBindModal = ref(false)
const selectedRole = ref({})

// 角色表单
const roleForm = ref({
  id: null,
  name: '',
  code: '',
  remark: '', // 修改为remark，匹配API参数
  status: 0 // 修改为数字类型，0表示启用，1表示禁用
})

// 菜单权限数据
const menuPermissions = ref([
  {
    id: 1,
    name: '仪表盘',
    checked: false,
    children: []
  },
  {
    id: 2,
    name: '用户管理',
    checked: false,
    children: [
      { id: 21, name: '用户列表', checked: false },
      { id: 22, name: '添加用户', checked: false },
      { id: 23, name: '编辑用户', checked: false },
      { id: 24, name: '删除用户', checked: false }
    ]
  },
  {
    id: 3,
    name: '角色管理',
    checked: false,
    children: [
      { id: 31, name: '角色列表', checked: false },
      { id: 32, name: '添加角色', checked: false },
      { id: 33, name: '编辑角色', checked: false },
      { id: 34, name: '删除角色', checked: false }
    ]
  },
  {
    id: 4,
    name: '菜单管理',
    checked: false,
    children: [
      { id: 41, name: '菜单列表', checked: false },
      { id: 42, name: '添加菜单', checked: false },
      { id: 43, name: '编辑菜单', checked: false },
      { id: 44, name: '删除菜单', checked: false }
    ]
  },
  {
    id: 5,
    name: '系统设置',
    checked: false,
    children: [
      { id: 51, name: '基本设置', checked: false },
      { id: 52, name: '安全设置', checked: false }
    ]
  }
])

// 操作权限数据
const operationPermissions = ref([
  { id: 101, name: '查看用户', checked: false },
  { id: 102, name: '添加用户', checked: false },
  { id: 103, name: '编辑用户', checked: false },
  { id: 104, name: '删除用户', checked: false },
  { id: 105, name: '封禁用户', checked: false },
  { id: 106, name: '解封用户', checked: false },
  { id: 107, name: '查看角色', checked: false },
  { id: 108, name: '添加角色', checked: false },
  { id: 109, name: '编辑角色', checked: false },
  { id: 110, name: '删除角色', checked: false },
  { id: 111, name: '查看菜单', checked: false },
  { id: 112, name: '添加菜单', checked: false },
  { id: 113, name: '编辑菜单', checked: false },
  { id: 114, name: '删除菜单', checked: false }
])

// 菜单树数据
const menuTree = ref([])

// 计算属性
const filteredRoles = computed(() => {
  let result = roles.value

  // 搜索过滤
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase()
    result = result.filter(role => 
      role.name.toLowerCase().includes(query) || 
      (role.remark && role.remark.toLowerCase().includes(query)) ||
      (role.description && role.description.toLowerCase().includes(query))
    )
  }

  // 状态过滤
  if (statusFilter.value) {
    result = result.filter(role => role.status === statusFilter.value)
  }

  return result
})

const totalPages = computed(() => {
  return Math.ceil(totalRoles.value / pageSize.value)
})

// 生命周期钩子
onMounted(() => {
  fetchRoles()
})

// 方法
const fetchRoles = async () => {
  try {
    // 调用API获取角色列表
    const params = {
      pageNo: currentPage.value,
      pageSize: pageSize.value
    }
    
    // 如果有搜索条件，添加到参数中
    if (searchQuery.value) {
      params.keyword = searchQuery.value
    }
    
    // 如果有状态筛选，添加到参数中
    if (statusFilter.value) {
      params.status = statusFilter.value
    }
    
    const response = await roleApi.getRolePage(params)
    
    // 处理响应数据
    roles.value = response.data.list || []
    totalRoles.value = response.data.total || 0
  } catch (error) {
    console.error('获取角色列表失败:', error)
    // 使用模拟数据作为后备
    roles.value = getMockRoles()
    totalRoles.value = roles.value.length
  }
}

const getMockRoles = () => {
  return [
    {
      id: 1,
      name: '超级管理员',
      code: 'super_admin',
      description: '拥有系统所有权限',
      status: 'active',
      createdAt: '2023-01-01T00:00:00Z',
      updatedAt: '2023-11-20T08:30:00Z'
    },
    {
      id: 2,
      name: '管理员',
      code: 'admin',
      description: '拥有大部分管理权限',
      status: 'active',
      createdAt: '2023-01-05T00:00:00Z',
      updatedAt: '2023-11-15T14:22:00Z'
    },
    {
      id: 3,
      name: '版主',
      code: 'moderator',
      description: '负责帖子内容管理',
      status: 'active',
      createdAt: '2023-02-10T00:00:00Z',
      updatedAt: '2023-11-10T16:45:00Z'
    },
    {
      id: 4,
      name: '普通用户',
      code: 'user',
      description: '基本用户权限',
      status: 'active',
      createdAt: '2023-03-15T00:00:00Z',
      updatedAt: '2023-11-05T09:15:00Z'
    },
    {
      id: 5,
      name: '访客',
      code: 'guest',
      description: '只读权限',
      status: 'inactive',
      createdAt: '2023-04-20T00:00:00Z',
      updatedAt: '2023-10-25T11:30:00Z'
    }
  ]
}

const handleSearch = () => {
  currentPage.value = 1
  fetchRoles()
}

const filterRoles = () => {
  currentPage.value = 1
  fetchRoles()
}

const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--
    fetchRoles()
  }
}

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++
    fetchRoles()
  }
}

const viewRole = (role) => {
  selectedRole.value = { ...role }
  showRoleDetailModal.value = true
}

const editRole = (role) => {
  roleForm.value = { ...role }
  showEditRoleModal.value = true
}

const saveRole = async () => {
  try {
    if (showEditRoleModal.value) {
      // 更新角色
      await roleApi.updateRole(roleForm.value.id, roleForm.value)
    } else {
      // 创建角色 - 不传递id参数，由后端自动生成
      const { id, ...roleData } = roleForm.value
      await roleApi.createRole(roleData)
    }
    
    closeModal()
    fetchRoles()
  } catch (error) {
    console.error('保存角色失败:', error)
  }
}

const editPermissions = async (role) => {
  selectedRole.value = { ...role }
  
  // 重置权限选择状态
  resetPermissions()
  
  try {
    // 加载角色的现有权限
    const permissions = await roleApi.getRolePermissions(role.id)
    setPermissionStates(permissions.data)
  } catch (error) {
    console.error('获取角色权限失败:', error)
  }
  
  showPermissionModal.value = true
}

const resetPermissions = () => {
  // 重置菜单权限
  menuPermissions.value.forEach(menu => {
    menu.checked = false
    if (menu.children) {
      menu.children.forEach(child => {
        child.checked = false
      })
    }
  })
  
  // 重置操作权限
  operationPermissions.value.forEach(permission => {
    permission.checked = false
  })
}

const setPermissionStates = (permissions) => {
  if (!permissions) return
  
  // 设置菜单权限状态
  if (permissions.menuIds) {
    menuPermissions.value.forEach(menu => {
      menu.checked = permissions.menuIds.includes(menu.id)
      if (menu.children) {
        menu.children.forEach(child => {
          child.checked = permissions.menuIds.includes(child.id)
        })
      }
    })
  }
  
  // 设置操作权限状态
  if (permissions.operationIds) {
    operationPermissions.value.forEach(permission => {
      permission.checked = permissions.operationIds.includes(permission.id)
    })
  }
}

const toggleMenuPermission = (menu) => {
  // 如果是父菜单，同时切换所有子菜单
  if (menu.children && menu.children.length > 0) {
    menu.children.forEach(child => {
      child.checked = menu.checked
    })
  }
}

const savePermissions = async () => {
  try {
    // 收集选中的权限
    const selectedMenuIds = []
    const selectedOperationIds = []
    
    // 收集菜单权限
    menuPermissions.value.forEach(menu => {
      if (menu.checked) {
        selectedMenuIds.push(menu.id)
      }
      if (menu.children) {
        menu.children.forEach(child => {
          if (child.checked) {
            selectedMenuIds.push(child.id)
          }
        })
      }
    })
    
    // 收集操作权限
    operationPermissions.value.forEach(permission => {
      if (permission.checked) {
        selectedOperationIds.push(permission.id)
      }
    })
    
    // 调用API保存权限
    await roleApi.updateRolePermissions(selectedRole.value.id, {
      menuIds: selectedMenuIds,
      operationIds: selectedOperationIds
    })
    
    showPermissionModal.value = false
  } catch (error) {
    console.error('保存权限失败:', error)
  }
}

const disableRole = async (role) => {
  if (confirm(`确定要禁用角色 ${role.name} 吗？`)) {
    try {
      await roleApi.disableRole(role.id)
      fetchRoles()
    } catch (error) {
      console.error('禁用角色失败:', error)
    }
  }
}

const enableRole = async (role) => {
  if (confirm(`确定要启用角色 ${role.name} 吗？`)) {
    try {
      await roleApi.enableRole(role.id)
      fetchRoles()
    } catch (error) {
      console.error('启用角色失败:', error)
    }
  }
}

const deleteRole = async (role) => {
  if (confirm(`确定要删除角色 ${role.name} 吗？此操作不可恢复！`)) {
    try {
      await roleApi.deleteRole(role.id)
      fetchRoles()
    } catch (error) {
      console.error('删除角色失败:', error)
    }
  }
}

const closeModal = () => {
  showAddRoleModal.value = false
  showEditRoleModal.value = false
  roleForm.value = {
    id: null,
    name: '',
    code: '',
    remark: '', // 修改为remark字段
    status: 0 // 修改为数字类型，0表示启用
  }
}

const getStatusText = (status) => {
  const statusMap = {
    0: '启用', // 修改为数字类型
    1: '禁用'  // 修改为数字类型
  }
  return statusMap[status] || status
}

const formatDate = (dateString) => {
  if (!dateString) return '从未'
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

// 角色绑定菜单相关方法
const bindMenus = async (role) => {
  selectedRole.value = { ...role }
  
  try {
    // 获取菜单树
    const menuResponse = await menuApi.getMenuDropdownTree()
    const processMenuData = (data) => {
      if (!data || !Array.isArray(data)) return []
      
      const result = []
      
      data.forEach(menu => {
        // 添加当前菜单到结果
        result.push({
          id: menu.id,
          parentId: menu.parentId === 0 ? null : menu.parentId,
          name: menu.name,
          icon: menu.icon,
          sort: menu.sort || 0,
          type: menu.type || 2,
          permission: menu.permission,
          status: menu.status || 0,
          checked: false // 默认未选中
        })
        
        // 递归处理子菜单
        if (menu.menuDropdownTreeResponseList && Array.isArray(menu.menuDropdownTreeResponseList)) {
          const childMenus = processMenuData(menu.menuDropdownTreeResponseList)
          result.push(...childMenus)
        }
      })
      
      return result
    }
    
    const flatMenus = processMenuData(menuResponse.data || [])
    menuTree.value = buildMenuTree(flatMenus)
    
    // 获取角色已绑定的菜单
    try {
      const roleMenuResponse = await roleApi.getRoleMenus(role.id)
      const roleMenuIds = roleMenuResponse.data || []
      
      // 设置菜单选中状态
      setMenuCheckStates(menuTree.value, roleMenuIds)
    } catch (error) {
      console.error('获取角色菜单失败:', error)
    }
    
    showMenuBindModal.value = true
  } catch (error) {
    console.error('获取菜单树失败:', error)
  }
}

// 构建菜单树
const buildMenuTree = (flatMenus) => {
  const menuMap = {}
  const rootMenus = []
  
  // 创建菜单映射
  flatMenus.forEach(menu => {
    menuMap[menu.id] = { ...menu, children: [] }
  })
  
  // 构建树形结构
  flatMenus.forEach(menu => {
    const menuNode = menuMap[menu.id]
    if (menu.parentId && menuMap[menu.parentId]) {
      menuMap[menu.parentId].children.push(menuNode)
    } else {
      rootMenus.push(menuNode)
    }
  })
  
  // 排序
  const sortMenus = (menus) => {
    menus.sort((a, b) => (a.sort || 0) - (b.sort || 0))
    menus.forEach(menu => {
      if (menu.children && menu.children.length > 0) {
        sortMenus(menu.children)
      }
    })
  }
  
  sortMenus(rootMenus)
  return rootMenus
}

// 设置菜单选中状态
const setMenuCheckStates = (menus, roleMenuIds) => {
  menus.forEach(menu => {
    menu.checked = roleMenuIds.includes(menu.id)
    
    if (menu.children && menu.children.length > 0) {
      setMenuCheckStates(menu.children, roleMenuIds)
    }
  })
}

// 切换菜单选中状态
const toggleMenuCheck = (menu) => {
  // 如果是父菜单，同时切换所有子菜单
  if (menu.children && menu.children.length > 0) {
    menu.children.forEach(child => {
      child.checked = menu.checked
    })
  }
}

// 保存菜单绑定
const saveMenuBindings = async () => {
  try {
    // 收集选中的菜单ID
    const selectedMenuIds = []
    
    const collectMenuIds = (menus) => {
      menus.forEach(menu => {
        if (menu.checked) {
          selectedMenuIds.push(menu.id)
        }
        
        if (menu.children && menu.children.length > 0) {
          collectMenuIds(menu.children)
        }
      })
    }
    
    collectMenuIds(menuTree.value)
    
    // 调用API保存菜单绑定
    await roleApi.updateRoleMenus(selectedRole.value.id, {
      menuIds: selectedMenuIds
    })
    
    showMenuBindModal.value = false
  } catch (error) {
    console.error('保存菜单绑定失败:', error)
  }
}

// 获取菜单类型文本
const getMenuTypeText = (type) => {
  const typeMap = {
    1: '目录',
    2: '菜单',
    3: '按钮'
  }
  return typeMap[type] || type
}
</script>

<style scoped>
.role-management {
  padding: 24px;
  background-color: #f8fafc;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-header h1 {
  color: #1e293b;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.btn-primary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background-color: #3b82f6;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-primary:hover {
  background-color: #2563eb;
}

/* 搜索和筛选区域 */
.filter-section {
  display: flex;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 16px;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 400px;
}

.search-box input {
  width: 100%;
  padding: 10px 16px 10px 40px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  background-color: white;
}

.search-box .icon-search {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #6b7280;
}

.filter-options {
  display: flex;
  gap: 12px;
}

.filter-options select {
  padding: 10px 16px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  background-color: white;
  min-width: 120px;
}

/* 表格样式 */
.table-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 24px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid #e5e7eb;
}

th {
  background-color: #f9fafb;
  color: #374151;
  font-weight: 600;
  font-size: 14px;
}

td {
  color: #1f2937;
  font-size: 14px;
}

tr:hover {
  background-color: #f9fafb;
}

/* 状态徽章 */
.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.active {
  background-color: #dcfce7;
  color: #166534;
}

.status-badge.inactive {
  background-color: #fef3c7;
  color: #92400e;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 8px;
}

.action-buttons button {
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 12px;
  white-space: nowrap;
}

.btn-view {
  background-color: #eff6ff;
  color: #1e40af;
}

.btn-view:hover {
  background-color: #dbeafe;
}

.btn-edit {
  background-color: #f0fdf4;
  color: #166534;
}

.btn-edit:hover {
  background-color: #dcfce7;
}

.btn-permissions {
  background-color: #f3e8ff;
  color: #7c3aed;
}

.btn-permissions:hover {
  background-color: #e9d5ff;
}

.btn-menu-bind {
  background-color: #ecfdf5;
  color: #059669;
}

.btn-menu-bind:hover {
  background-color: #d1fae5;
}

.btn-disable {
  background-color: #fef2f2;
  color: #991b1b;
}

.btn-disable:hover {
  background-color: #fee2e2;
}

.btn-enable {
  background-color: #f0f9ff;
  color: #0369a1;
}

.btn-enable:hover {
  background-color: #e0f2fe;
}

.btn-delete {
  background-color: #fef2f2;
  color: #991b1b;
}

.btn-delete:hover {
  background-color: #fee2e2;
}

/* 分页控件 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
}

.btn-page {
  padding: 8px 16px;
  background-color: white;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  color: #374151;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-page:hover:not(:disabled) {
  background-color: #f9fafb;
}

.btn-page:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #6b7280;
  font-size: 14px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.large-modal {
  max-width: 800px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #e5e7eb;
}

.modal-header h2 {
  margin: 0;
  color: #1f2937;
  font-size: 18px;
  font-weight: 600;
}

.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #6b7280;
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-body {
  padding: 24px;
}

/* 表单样式 */
.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #374151;
  font-weight: 500;
  font-size: 14px;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
}

.form-group textarea {
  resize: vertical;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.btn-cancel {
  padding: 8px 16px;
  background-color: white;
  color: #374151;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-cancel:hover {
  background-color: #f9fafb;
}

/* 角色详情样式 */
.role-detail {
  margin-bottom: 24px;
}

.detail-item {
  display: flex;
  padding: 12px 0;
  border-bottom: 1px solid #f3f4f6;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-item .label {
  width: 120px;
  color: #6b7280;
  font-weight: 500;
}

.detail-item .value {
  flex: 1;
  color: #1f2937;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}

/* 权限设置样式 */
.permission-section {
  margin-bottom: 24px;
}

.permission-section h3 {
  margin: 0 0 16px 0;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
}

.permission-tree {
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 16px;
  background-color: #f9fafb;
}

.permission-item {
  margin-bottom: 12px;
}

.permission-item.child {
  margin-left: 24px;
  margin-top: 8px;
}

.permission-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.permission-header input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.permission-header label {
  margin: 0;
  cursor: pointer;
  font-weight: 500;
}

.permission-children {
  margin-top: 8px;
}

.permission-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.permission-checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
}

.permission-checkbox input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.permission-checkbox label {
  margin: 0;
  cursor: pointer;
}

/* 角色绑定菜单样式 */
.menu-bind-section {
  margin-bottom: 24px;
}

.menu-bind-section h3 {
  margin: 0 0 16px 0;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
}

.menu-tree {
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 16px;
  background-color: #f9fafb;
  max-height: 400px;
  overflow-y: auto;
}

.menu-item {
  margin-bottom: 12px;
}

.menu-item.child {
  margin-left: 24px;
  margin-top: 8px;
}

.menu-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-header input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.menu-header label {
  margin: 0;
  cursor: pointer;
  font-weight: 500;
  flex: 1;
}

.menu-children {
  margin-top: 8px;
}
</style>